<?php
/**
 * 码支付异步
 */

require_once("../../includes/fun.global.php");

ksort($_REQUEST); //排序post参数
reset($_REQUEST); //内部指针指向数组中的第一个元素
$sign = '';
foreach ($_REQUEST AS $key => $val) {
    if ($val == '') continue;
    if ($key != 'sign') {
        if ($sign != '') {
            $sign .= "&";
            $urls .= "&";
        }
        $sign .= "$key=$val"; //拼接为url参数形式
        $urls .= "$key=" . urlencode($val); //拼接为url参数形式
    }
}

if ($conf['pay_qqapy'] <> 2 && $conf['pay_wxpay'] <> 2 && $conf['pay_alipay'] <> 2) { #如果都未开启
    die('fail');
} elseif (empty($_REQUEST['pay_no']) || md5($sign . $conf['cpay_key']) <> $_REQUEST['sign']) { //sign不合法！
    die('fail');
} else { //成功返回

    $order = (int)$_QET['pay_id'];
    $money = (float)$_REQUEST['price'];

    $srow = $DB->get_row("SELECT * FROM sky_pay WHERE `order`='$order' limit 1 ");

    if ($srow['state'] == 2 && $srow['money'] >= $money) {
        $acc = $DB->get_row("SELECT * FROM `sky_order` WHERE `order` = '$order' ORDER BY `id` DESC LIMIT 1");
        if ($acc['order'] <> ''&&$acc) die('success');
        //付款完成后，支付系统发送该交易状态通知
        $data = [
            'out_trade_no' => $order, //本地订单
            'money' => $money, //付款金额
            'trade_no' => (int)$_REQUEST['pay_no'], //云订单
        ];
        \pay::success($data, 2);
    } else echo "success";
}